﻿<?php
function relacionadas($pelicula)
{
	$apikey = 'evp42bcxmcj6j2pnggapcwvt';
	$q = urlencode($pelicula); // make sure to url encode an query parameters
	// construct the query with our apikey and the query we want to make
	$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey='.$apikey.'&q='.$q;
	// setup curl to make a call to the endpoint
	$session = curl_init($endpoint);
	// indicates that we want the response back
	curl_setopt($session,CURLOPT_RETURNTRANSFER,true);
	// exec curl and get the data back
	$data = curl_exec($session);
	// remember to close the curl session once we are finished retrieveing the data
	curl_close($session);
	// decode the json data to make it easier to parse the php
	$search_results = json_decode($data);
	if ($search_results === NULL) 
	{
		return null;
	}
	if(count($search_results->movies)==0)
		return null;
	// play with the data!
	$movies = $search_results->movies;
	$movie=$movies[0];
	$id=$movie->id;
	$relacionadas=$movie->links->similar;
	$relacionadas=$relacionadas.'?apikey='.$apikey.'&limit=5';
	//var_dump($relacionadas);
	
	$session2 = curl_init($relacionadas);
	curl_setopt($session2,CURLOPT_RETURNTRANSFER,true);
	$data2 = curl_exec($session2);
	curl_close($session2);
	$search_results2 = json_decode($data2);
	if ($search_results2 === NULL) 
		{die('Error parsing json');return;}
	$movies = $search_results2->movies;
	
	$peliculas = array();
	$cont=0;
	foreach ($movies as $movie){
		$peliculas[$cont]=array('title'=>$movie->title,'id'=>$movie->id);
		$cont=$cont+1;
	}
	return $peliculas;
}	


function webfilm($id = null){
	$apikey = 'evp42bcxmcj6j2pnggapcwvt';
	// construct the query with our apikey and the query we want to make
	$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies/'.$id.'.json?apikey='.$apikey;
	// setup curl to make a call to the endpoint
	$session = curl_init($endpoint);
	// indicates that we want the response back
	curl_setopt($session,CURLOPT_RETURNTRANSFER,true);
	// exec curl and get the data back
	$data = curl_exec($session);
	// remember to close the curl session once we are finished retrieveing the data
	curl_close($session);
	// decode the json data to make it easier to parse the php
	$search_results = json_decode($data);
	if ($search_results === NULL) 
	{
		return null;
	}
	//if(count($search_results->movies)==0)
	//	return null;
	// play with the data!
	return $search_results;	
}

function webfilmrelated($id = null){
	$apikey = 'evp42bcxmcj6j2pnggapcwvt';
	// construct the query with our apikey and the query we want to make
	$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies/'.$id.'/similar.json?apikey='.$apikey.'&limit=5';
	// setup curl to make a call to the endpoint
	$session = curl_init($endpoint);
	// indicates that we want the response back
	curl_setopt($session,CURLOPT_RETURNTRANSFER,true);
	// exec curl and get the data back
	$data = curl_exec($session);
	// remember to close the curl session once we are finished retrieveing the data
	curl_close($session);
	// decode the json data to make it easier to parse the php
	$search_results = json_decode($data);
	if ($search_results === NULL) 
	{
		return null;
	}
	return $search_results->movies;
}
?>